import { getListApi } from "@/api/reportManager/customerBalance";
import CommonHook from "@/hook/commonHook";
import { computed, Ref } from "vue";
export default function OperationHook(state: {
  searchForm: CustomerBalance.SearchForm;
  tableData: Ref<CustomerBalance.ListRows[]>;
  loading: Ref<boolean>;
  tableColumn: Ref<Array<SuperTable.TableCoulmnItem | SuperTable.NestTableColumnItem>>;
  asyncTable: Ref<boolean>;
  drawerOpts: Base.DrawerOpts;
  tableRef: Ref<any>;
}) {
  const { searchForm, tableData, loading, tableColumn, asyncTable, drawerOpts, tableRef } = state;
  const { exportJsonToExcel } = CommonHook();

  const selectRows = computed(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const getData = () => {
    const { shortNameList } = searchForm;
    const params: Parameters<typeof getListApi>[0] = {
      ...searchForm
    };
    if (shortNameList) {
      params.shortNameList = shortNameList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    } else {
      params.shortNameList = [];
    }

    loading.value = true;
    return getListApi(params)
      .then((res: any) => {
        const { code } = res;
        const { tableDataList, tableHeadList, total } = res.data;
        if (code === 200) {
          const tableHeader = tableHeadList.filter((t: CustomerBalance.TableHeaderColumn) => t.showFlag === 1);
          tableColumn.value = tableHeader.map((t: CustomerBalance.TableHeaderColumn) => {
            return {
              field: t.fieldCode,
              title: t.fieldName,
              minWidth: "120"
            };
          });
          tableColumn.value.unshift({
            type: "checkbox",
            field: "checkbox",
            minWidth: "50"
          });

          tableColumn.value.push({
            field: "action",
            title: "操作",
            minWidth: "100",
            fixed: "right",
            renderSlot: "action"
          });
          tableData.value = tableDataList || [];
          searchForm.total = Number(total);
          asyncTable.value = true;
        }
      })
      .finally(() => (loading.value = false));
  };

  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };

  //详情
  const detailsHandle = (row: CustomerBalance.ListRows) => {
    drawerOpts.title = `详情`;
    drawerOpts.width = "1200px";
    // drawerOpts.type = 2;
    drawerOpts.component = "Details";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  // 导出
  const exportDataHandle = () => {
    const exportField = tableColumn.value.filter((t: any) => t.title && t.title !== "操作");
    const header = [
      exportField.map((t: any) => {
        return t.title;
      })
    ];
    if (selectRows.value.length) {
      const data = selectRows.value.map((t: CustomerBalance.ListRows) => {
        const result: any = {};
        for (let i = 0; i < exportField.length; i++) {
          const target = exportField[i];
          result[target.field] = (t as any)[target.field];
        }
        return result;
      });
      loading.value = true;
      exportJsonToExcel(header, data, "客户余额");
      loading.value = false;
    } else {
      const { shortNameList } = searchForm;
      const params: Parameters<typeof getListApi>[0] = {
        ...searchForm,
        pageSize: 9999999
      };
      if (shortNameList) {
        params.shortNameList = shortNameList
          .trim()
          .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
          .split(",");
      } else {
        params.shortNameList = [];
      }
      loading.value = true;
      return getListApi(params)
        .then((res: any) => {
          const { code } = res;
          const { tableDataList } = res.data;
          if (code === 200) {
            const data = tableDataList.map((t: CustomerBalance.ListRows) => {
              const result: any = {};
              for (let i = 0; i < exportField.length; i++) {
                const target = exportField[i];
                result[target.field] = (t as any)[target.field];
              }
              return result;
            });
            loading.value = true;
            exportJsonToExcel(header, data, "客户余额");
            loading.value = false;
          }
        })
        .catch(() => (loading.value = false));
    }
  };

  return {
    getData,
    searchHandle,
    detailsHandle,
    exportDataHandle
  };
}
